package com.webshop.Dao;

import com.webshop.database.ConnPool;
import com.webshop.database.JDBCUtil;
import com.webshop.domain.Business;
import com.webshop.domain.user.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BusinessDao {
    public static void addBusiness(Business business){
        Connection conn = ConnPool.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "insert into business values(?,?,?,?,null);";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,business.getItems());
            ps.setString(2,business.getNums());
            ps.setDouble(3,business.getSum());
            ps.setString(4,business.getUsername());
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn,ps,rs);
        }
    }
    public static List<Business> getbussinesses(User user){
        Connection conn = ConnPool.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Business> businesses = new ArrayList<Business>();
        String sql = "select * from business where whos=?;";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            rs = ps.executeQuery();
            while (rs.next()){
                Business business = new Business(user.getUsername());
                business.setItems(rs.getString("items"));
                business.setNums(rs.getString("nums"));
                business.setSum(rs.getDouble("sum"));
                business.setId(rs.getInt("id"));
                businesses.add(business);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn,ps,rs);
            return businesses;
        }
    }
    public static Business getBusiness(int id){
        Connection conn = ConnPool.getConn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Business business = null;
        String sql = "select * from business where id=?;";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while (rs.next()){
                business = new Business();
                business.setItems(rs.getString("items"));
                business.setNums(rs.getString("nums"));
                business.setSum(rs.getDouble("sum"));
                business.setId(rs.getInt("id"));
                business.setUsername(rs.getString("whos"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn,ps,rs);
            return business;
        }
    }

//    public static void main(String[] args) {
//        Business business = BusinessDao.getBusiness(3);
//        System.out.println(business);
//    }
}
